package com.example.buxiaorui.studentsystem;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class RegistActivity extends AppCompatActivity {
    private TextView tv_title;
    private EditText et_username;
    private EditText et_password;
    private EditText et_password_confirm;
    private EditText et_code;
    private Boolean regist_status;
    private EditText tips_text;
    private DialogManager dialogManager;
    private SqlDao sqlDao;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.regist);
        tv_title=(TextView)findViewById(R.id.title);
        tv_title.setText("");
        et_username=(EditText)findViewById(R.id.regist_user);
        et_password=(EditText)findViewById(R.id.regist_password);
        et_password_confirm=(EditText)findViewById(R.id.regist_password_confirm);
        et_code=(EditText)findViewById(R.id.regist_code);
        sqlDao=new SqlDao(this);
        //账号判断
        et_username.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View view, boolean b) {
                if (b){
                    et_username.setTextColor(Color.BLACK);
                }else {
                    SqlDao sqlDao=new SqlDao(RegistActivity.this);
                    boolean res=sqlDao.checkdata(et_username.getText().toString().trim());
                    if (res==true){
                        DialogManager dialogManager=new DialogManager(RegistActivity.this);
                        dialogManager.showUserexist();
                        et_username.setTextColor(Color.RED);
                    }
                }
            }
        });
        //密码判断
        et_password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){
                    //获得焦点
                }else {
                    //失去焦点时判断密码是否符合格式
                    String password=et_password.getText().toString().trim();
                    if (password.length()<4||password.length()>8){
                        DialogManager dialogManager=new DialogManager(RegistActivity.this);
                        dialogManager.showErrorpassword();
                    }
                }
            }
        });
        //邀请码判断
        et_code.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){

                }else {
                    //失去焦点时判断是否存在code
                    String code=et_code.getText().toString();
                    SqlDao sqlDao=new SqlDao(RegistActivity.this);
//                    boolean res=sqlDao.checkCode(code);
//                    if (res==false){
//                        DialogManager dialogManager=new DialogManager(RegistActivity.this);
//                        dialogManager.showErrorCode();
//                    }
                }
            }
        });
        //重复密码判断
        et_password_confirm.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b){

                }else{
                    boolean b1=confim_password();
                    if (b1==false){
                        dialogManager=new DialogManager(RegistActivity.this);
                        dialogManager.showError();
                    }
                }
            }
        });
    }
    //设置页面结束动画为无
    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }
    public void back(View view){
        finish();
    }

    //判断重复密码是否一致
    public boolean confim_password(){
        boolean res=false;
        String pd1=et_password.getText().toString().trim();
        String pd2=et_password_confirm.getText().toString().trim();
        if (TextUtils.equals(pd1,pd2)){
            res=true;
        }
        return res;
    }

    //注册
    public void regist(View view){
        boolean status=checkRegistStatus();
        if (status){
            String username=et_username.getText().toString().trim();
            String password=et_password.getText().toString().trim();
            int code=(int)((Math.random()*9+1)*1000);
            sqlDao.addManager(username,password,code);
            boolean res=sqlDao.checkdata(username);
            if (res){
                Intent intent=new Intent(this,NotificationActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }
    public boolean checkRegistStatus(){
        boolean status=false;
        String username=et_username.getText().toString().trim();
        String password=et_password.getText().toString().trim();
        String password_confirm=et_password_confirm.getText().toString().trim();
        String mcode=et_code.getText().toString().trim();
        boolean username_check=sqlDao.checkdata(username);
        boolean code_check=sqlDao.checkCode(mcode);
        boolean password_check=true;
        DialogManager dialogManager=new DialogManager(RegistActivity.this);
        if (TextUtils.equals(password,password_confirm)==false){
            password_check=false;
        }
        if (!username_check&&code_check&&password_check){
            status=true;
        }else if (!code_check){
            dialogManager.showErrorCode();
        }else if (username_check){
            dialogManager.showUserexist();
        }else if (password_check){
            dialogManager.showError();
        }else if (TextUtils.isEmpty(username)||TextUtils.isEmpty(password)||TextUtils.isEmpty(mcode)||TextUtils.isEmpty(password_confirm)){
            dialogManager.showNotNull();
        }

        return status;
    }

}

